/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.latestbit.smartobjects.smartidlc.beautifiers;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.latestbit.smartobjects.smartidlc.ICodeBeautifier;
import org.latestbit.smartobjects.smartidlc.IMessageReporter;

public class AstyleBeautifier implements ICodeBeautifier {

	private String processName = "astyle";
	
	@SuppressWarnings("unused")
	private String pathToConfig;
	
	private IMessageReporter reporter;

	public AstyleBeautifier(String pathToConfig, IMessageReporter reporter) throws Exception {
		this.pathToConfig = pathToConfig;
		this.reporter = reporter;
		if(System.getProperty("os.name").equalsIgnoreCase("windows")) {
			this.processName = this.processName + ".exe";
		}
		try {
			reporter.onVerboseMessage("Trying to find "+processName+" as code beautifier");
			Process proc = Runtime.getRuntime().exec(processName+" --help");
			proc.waitFor();			
		}
		catch(Exception ex) {
			reporter.onVerboseMessage("Trying to find "+pathToConfig+processName+" as code beautifier");
			Process proc = Runtime.getRuntime().exec(pathToConfig+processName+" --help");
			proc.waitFor();
			this.processName = pathToConfig+processName;
		}
		reporter.onVerboseMessage("Found: "+processName);		
	}
	
	@Override
	public void beautify(Set<String> fileSet, String language) throws Exception {
		List<String> commandLineArgs = new ArrayList<String>();
		commandLineArgs.add(this.processName);
		commandLineArgs.add("-x");
		commandLineArgs.add("-U");
		commandLineArgs.add("-n");
		commandLineArgs.add("-F");
		commandLineArgs.addAll(fileSet);
		ProcessBuilder builder = new ProcessBuilder(commandLineArgs);		
		
		// reporter.onVerboseMessage("Executing code beautifier on "+pathTofile);		
		Process proc = builder.start();
		
		if(reporter.isVeryVerboseOutputEnabled()) {
			BufferedReader inputStream = 
				new BufferedReader(new InputStreamReader(proc.getInputStream()));
			BufferedReader errorStream = 
				new BufferedReader(new InputStreamReader(proc.getErrorStream()));
			 
	        String line=null;
	
	        while((line=inputStream.readLine()) != null) {
	            reporter.onVeryVerboseMessage(" ( uncrustify-verbose ) -> "+line);
	        }        
	        while((line=errorStream.readLine()) != null) {
	            reporter.onVeryVerboseMessage("( uncrustify ) ->"+line);
	        }
		}
		proc.waitFor();
	}

	@Override
	public boolean isLanguageSupported(String languageName) {
		if(languageName.equalsIgnoreCase("java") 
		   || languageName.equalsIgnoreCase("cpp") || languageName.equalsIgnoreCase("c++")
		   || languageName.equalsIgnoreCase("c")
		   || languageName.equalsIgnoreCase("c#")
		)
			return true;
		else
			return false;
	}

}
